package com.atguigu.system.service.impl;

import com.atguigu.system.mapper.PurchaseManagementMapper;
import com.atguigu.system.model.entity.PurchaseManagement;
import com.atguigu.system.model.entity.Sku;
import com.atguigu.system.model.entity.Spu;
import com.atguigu.system.service.PurchaseManagementService;
import com.atguigu.system.service.SkuService;
import com.atguigu.system.service.SpuService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2025-03-02
 */
@Service
public class PurchaseManagementServiceImpl extends ServiceImpl<PurchaseManagementMapper, PurchaseManagement> implements PurchaseManagementService {


    @Autowired
    private SkuService skuService;

    @Autowired
    private SpuService spuService;
    @Override
    public Page<PurchaseManagement> pageQuery(Page<PurchaseManagement> page, Integer supplierId, Integer productId, String purchaseStatus) {
        LambdaQueryWrapper<PurchaseManagement> queryWrapper = new LambdaQueryWrapper<>();
        if (supplierId != null) {
            queryWrapper.eq(PurchaseManagement::getSupplierId, supplierId);
        }
        if (productId != null) {
            queryWrapper.eq(PurchaseManagement::getProductId, productId);
        }
        if (purchaseStatus != null && !purchaseStatus.isEmpty()) {
            queryWrapper.eq(PurchaseManagement::getPurchaseStatus, purchaseStatus);
        }

        queryWrapper.eq(PurchaseManagement::getIsDeleted, 0); // 只查询未删除的记录
        Page<PurchaseManagement> purchaseManagementPage = baseMapper.selectPage(page, queryWrapper);

        // 库存SKU改成具体信息
        List<PurchaseManagement> records = purchaseManagementPage.getRecords();
        records.forEach(i->{
            String desc = null;
            Sku byId = skuService.selectByID(i.getProductId());
            Spu spu = spuService.selectByID(byId.getSpuId());

            String spuName = spu.getSpuName();
            String brand = spu.getBrand();
            desc = spuName+"-"+brand+"-"+byId.getColor()+"-"+byId.getSize()+"-"+byId.getQuality();
            i.setSkuName(desc);

        });
        purchaseManagementPage.setRecords(records);
        return purchaseManagementPage;
    }
}
